function export2()
%
% function to export data traces to a binary file to be read by Igor Pro.
%
% Takes current selection and generates a file (or files) named:
% filename_frec.dat
% provides a list of headers "Record(VorI)nn"
% Modified so that headers can be customized ..
% if the channel list is 2 elements, write V and I
% if the channel list is more, write a,b,c,d, etc.
% Filename is "Prefix""[V,I]""recordnumber"
%
global   CONTROL DFILE ALLCH


prompt={'ASCII (A), Binary (B) or Matlab (M):', ...
    'Channel List (ex: 1,2,3 or 1:4):', ...
    'Prefix'};
def={'A','[1,2]', 'Record'};
dlgTitle='Export Traces';
lineNo=1;
answer=inputdlg(prompt,dlgTitle,lineNo,def);
mode = answer{1};
chlist = eval(['[' answer{2} '];']);
prefix = answer{3};

sfi = getmainselection;

for i = 1:length(sfi)
    sf = sfi(i);
    [DFILE, DPARS, err] = ana_setup(DFILE, sf);
    if(err)
        return;
    end;
    S = load(DFILE.fullfile, '-regexp', '[sd]f[0-9]');
    % set this according to the platform you are running on:
    % mac:
    switch computer
        case {'MAC', 'MACI'}
            [err homedir] = system('echo $HOME');
            opath = sprintf('%s/%s', homedir(1:end-1), 'Desktop/');
            % PC:
        case 'PCWIN'
            opath = 'c:\data\extracted\';
        otherwise % assume glnx86 ...
            opath = '/home/pmanis/extracteddata';
    end;


    time = make_time(DFILE);


    if(findstr(mode, ['A', 'B']))
        fn = sprintf('%s%s_%d.dat', opath, CONTROL(sf).filename, CONTROL(sf).recbeg);
        fn
        hw=fopen(fn, 'w');
        if(isempty(hw) || hw <= 0)
            fprintf(1, 'Unable to open output file %s', fn);
            return;
        end;
    else
        fn = sprintf('%s%s_%d.mat', opath, CONTROL(sf).filename, CONTROL(sf).recbeg);
    end;

    s = size(ALLCH{1});
    t = size(time); %#ok<NASGU>
    switch(mode)
        case 'A'
            fprintf(hw, '%% File: %s  Rate: %7.3f  Frec: %d  Channels#: ', ...
                CONTROL(sf).filename, floor(DFILE.rate(1)*DFILE.nr_channel(1)), CONTROL(sf).recbeg);
            for k = 1:length(chlist)
                fprintf(hw, '%d ', chlist(k));
            end;
            fprintf(hw, '\r\n');
            fprintf(hw, '"t%d"', CONTROL(sf).recbeg);
          %  fprintf(1, '"t%d"', CONTROL(sf).recbeg);
            for k = 1:length(chlist)
                for j = 1:s(1)
                    fprintf(hw, '\t"%sc%1dr%d"', prefix, k, (CONTROL(sf).recbeg+j-1)); % titles of variables for igor
            %        fprintf(1, '\t"%sc%1dr%d"', prefix, k, (CONTROL(sf).recbeg+i-1)); % titles of variables for igor
                end;
            end;
            fprintf(hw, '\r\n');
           % fprintf(1, '\r\n');
        otherwise
    end;
    for j = 1:size(time,2) % now we write the data array
        switch(mode)
            case 'B'
                fwrite(hw, time(1,j), 'real*8');
                fwrite(hw, S(:,j), 'real*8');
            case 'A'
                fprintf(hw, '%f', time(1, j));
                for k = 1:length(chlist)
                    fprintf(hw, '\t%f', ALLCH{k}(:,j));
                end;
                fprintf(hw, '\r\n');
            case 'M'
                d = [time(1,:); S]'; %#ok<NASGU>
                save(fn, 'd', '-v6');
            otherwise
        end;
    end;
    fclose(hw);
    QueMessage(sprintf('Traces exported to: %s ', fn));

end;



